package com.lawyer.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lawyer.entity.User;
import com.lawyer.model.Message;
import com.lawyer.model.MessageConstant;
import com.lawyer.repository.RegimentalMemberRepository;
import com.lawyer.repository.UserRepository;

@RestController
@RequestMapping(value = "/agentImage")
@Api(tags = "微信代理二维码")
@SuppressWarnings("all")
public class ImageController {

	@Autowired
	UserRepository userRepository;

	@Autowired
	RegimentalMemberRepository regimentalMemberRepository;

	@ApiOperation(httpMethod = "GET", value = "微信代理二维码", notes = "微信代理二维码")
	@RequestMapping(value = "/agentQrCode", method = RequestMethod.GET)
	public void myQrCode(String userId, HttpServletRequest request, HttpServletResponse response) {
		Message msg = new Message();

		User user = userRepository.findOne(userId);

		String qrCode = null;
		try {
			OutputStream os = null;
			try {
				// 读取图片
				BufferedImage image = ImageIO.read(new URL(user.getQrCode()));
				response.setContentType("image/png");
				os = response.getOutputStream();

				if (image != null) {
					ImageIO.write(image, "png", os);
				}
			} catch (IOException e) {
			} finally {
				if (os != null) {
					os.flush();
					os.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			msg.setCode(MessageConstant.FAILED_CODE);
			msg.setMessage(MessageConstant.FAILED_MSG);
		}

	}


}
